/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.service.impl;

import com.workcell.workdata.im.constant.MessageActionGroup;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.entity.MessageIndex;
import com.workcell.workdata.im.repository.MessageIndexRepository;
import com.workcell.workdata.im.repository.MessageRepositoryProxy;
import com.workcell.workdata.im.service.MessageIndexService;
import com.workcell.workdata.im.service.MessageService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Service
public class MessageIndexServiceImpl implements MessageIndexService {

	@Resource
	private MessageService messageService;

	@Resource
	private MessageIndexRepository messageIndexRepository;

	@Resource
	private MessageRepositoryProxy messageRepositoryProxy;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(Message message) {
		this.add(message, Collections.singletonList(message.getReceiver()));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(Message message, List<Long> uidList) {

		if (MessageActionGroup.TRANSIENT.contains(message.getAction())){
			return;
		}

		messageService.save(message);

		List<MessageIndex> indices = new LinkedList<>();
		for (Long uid : uidList){
			MessageIndex index = new MessageIndex();
			index.setCreateTime(message.getCreateTime());
			index.setMid(message.getId());
			index.setUid(uid);
			index.setAction(message.getAction());
			indices.add(index);
		}

		messageRepositoryProxy.save(indices);
	}

	@Override
	@Async("saveTaskExecutor")
	public void delete(Long uid) {
		messageIndexRepository.delete(uid);
	}

	@Override
	@Async("saveTaskExecutor")
	public void delete(Long uid, String action) {
		messageIndexRepository.delete(uid,action);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@Async("saveTaskExecutor")
	public void receive(Long uid, long mid,String action) {
		messageIndexRepository.delete(uid,mid);
		messageService.receive(mid,action);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@Async("saveTaskExecutor")
	public void delete(List<Long> uidList, long mid) {
		messageIndexRepository.delete(uidList.toArray(new Long[0]),mid);
	}

	@Override
	@Async("saveTaskExecutor")
	public void revoke(long uid,long mid, String action) {
		messageService.revoke(uid,mid,action);
	}
}
